//
//  ProgramsInfoViewController.m
//  SimplystylePictorial
//
//  Created by Mac on 15/7/7.
//  Copyright (c) 2015年 Hu. All rights reserved.
//

#import "ProgramsInfoViewController.h"
#import "Header.h"

@interface ProgramsInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView ;
}
@property (nonatomic,copy)NSMutableArray* itemArray;
@property (nonatomic,copy)NSMutableArray* infoArray;
@end

@implementation ProgramsInfoViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [_tableView addPullToRefreshWithActionHandler:^{
        [self performSelector:@selector(downLoad) withObject:self afterDelay:1.2f];
        [self getRequestWith:self.ID];
    }];
}

- (void)downLoad
{
    [_tableView.pullToRefreshView stopAnimating];
    _tableView.frame =CGRectMake(0, 64, kScreenW, kScreenH-64);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    
    self.itemArray = [[NSMutableArray alloc]initWithCapacity:0];
    self.infoArray = [[NSMutableArray alloc]initWithCapacity:0];
    [self getRequestWith:self.ID];
    
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self ;
    _tableView.separatorStyle = NO ;
    [self.view addSubview:_tableView];
}

- (void)getRequestWith:(NSString *)idString
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manger.responseSerializer setValue:@"api-client/1.0 com.mmmono.mono/1.7.1 iOS/8.1 iPhone7,2" forKey:@"X-Device-City"];
    [manager.requestSerializer setValue:@"api-client/1.0 com.mmmono.mono/1.7.1 iOS/8.1 iPhone7,2" forHTTPHeaderField:@"X-Device-City"];
    [manager.requestSerializer setValue:@"f4c98f42161811e5a03e525400f61f41" forHTTPHeaderField:@"HTTP-AUTHORIZATION"];
    
    NSString *urlString = [NSString stringWithFormat:@"http://mmmono.com/api/program/%@/list?page=1",self.ID];
    NSLog(@"%@",self.ID);
    
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.itemArray = responseObject[@"items"];
        NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];
        for (NSDictionary *dict in self.itemArray)
        {
            imageModel* model = [imageModel setInfoWithDict:dict];
            [array addObject:model];
        }
        self.infoArray = array;
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"sorry,网络发生错误" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    imageModel*model=self.infoArray[indexPath.row];
    if (self.infoArray.count>0) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
    if (model.hot) {
        UIImageView*cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kScreenW-10, (kScreenW-10)*3/5)];
        cellImageView.clipsToBounds=YES;
        cellImageView.contentMode=UIViewContentModeScaleAspectFill;
        [cellImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_url]];
        [cell addSubview:cellImageView];
        
        UIImageView*cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, ((kScreenW-10)*3/5+15), 30, 50)];
        cellImage.image=[UIImage imageNamed:@"icon-hot"];
        cellImage.clipsToBounds=YES;
        [cell addSubview:cellImage];
        
        UILabel*titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+10), kScreenW-50, 30)];
        titleLabel.text=model.title;
        titleLabel.font=[UIFont boldSystemFontOfSize:16];
        [cell addSubview:titleLabel];
        
        UILabel*textLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+35), kScreenW-60, 60)];
        textLabel.text=model.text;
        textLabel.numberOfLines=0;
        textLabel.font=[UIFont boldSystemFontOfSize:13];
        [cell addSubview:textLabel];
        
        UILabel*authorLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+85), 200, 30)];
        authorLabel.text=[NSString stringWithFormat:@"🍭%@/%@   %@",model.read_count,model.action_count,model.author];
        authorLabel.numberOfLines=0;
        authorLabel.font=[UIFont boldSystemFontOfSize:12];
        [cell addSubview:authorLabel];
        
    }if (!model.hot) {
        UIImageView*cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kScreenW/3+20, (kScreenW/3+20)*3/4-10)];
        cellImageView.clipsToBounds=YES;
        cellImageView.contentMode=UIViewContentModeScaleAspectFill;
        [cellImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_url]];
        [cell addSubview:cellImageView];
        
        UILabel*titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3+30, 5, kScreenW-kScreenW/3-35, 20)];
        titleLabel.text=[NSString stringWithFormat:@"%@-%@",model.programName,model.author];
        titleLabel.font=[UIFont boldSystemFontOfSize:10];
        [cell addSubview:titleLabel];
        
        UILabel*textLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3+30, 20, kScreenW-kScreenW/3-35, 50)];
        textLabel.text=model.title;
        textLabel.numberOfLines=0;
        textLabel.font=[UIFont boldSystemFontOfSize:14];
        [cell addSubview:textLabel];
        
        UILabel*authorLabel=[[UILabel alloc]initWithFrame:CGRectMake(kScreenW/3+30, (65), 200, 30)];
        authorLabel.text=[NSString stringWithFormat:@"🎈%@/%@",model.read_count,model.action_count];
        authorLabel.numberOfLines=0;
        authorLabel.font=[UIFont boldSystemFontOfSize:12];
        [cell addSubview:authorLabel];
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    imageModel *model = self.infoArray[indexPath.row];
    if(model.hot)
    {
        return (kScreenW-10)*3/5+120;
    }
    else
        return (kScreenW/3+20)*3/4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictorialWebViewViewController *webVC = [[PictorialWebViewViewController alloc]init];
    webVC.hidesBottomBarWhenPushed = YES ;
    imageModel*model=self.infoArray[indexPath.row];
    webVC.title=model.title;
    webVC.webUrl=model.item_url;
    [self.navigationController pushViewController:webVC animated:YES];
}
@end
